package com.xqboss.web.controller.user;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.user.UserIdentityPermBasic;
import com.xqboss.apps.domain.user.UserIdentityPermWrenchProduction;
import com.xqboss.apps.domain.user.UserIdentityRank;
import com.xqboss.apps.dto.user.DeleteWrenchProductionDto;
import com.xqboss.apps.dto.user.OpenOrCloseWrenchFactoryDto;
import com.xqboss.apps.dto.user.UpdateWrenchFactoryConfigDto;
import com.xqboss.apps.service.user.UserIdentityPermBasicService;
import com.xqboss.apps.service.user.UserIdentityPermWrenchProductionService;
import com.xqboss.apps.service.user.UserIdentityRankService;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 扳手生产配置
 * </p>
 *
 * @author 木鱼
 * @since 2024-01-09
 */
@RestController
@Slf4j
@Api(tags = "扳手套餐")
@RequestMapping("/web/userIdentityPermWrenchProduction")
@RequiredArgsConstructor
public class UserIdentityPermWrenchProductionController extends BaseController {
    private final UserIdentityPermWrenchProductionService basicService;
    @Autowired
    private UserIdentityRankService userIdentityRankService;

    /**
     * 列表查询
     */
    @GetMapping("/list")
    @ApiOperation(" 扳手套餐列表查询")
    @PreAuthorize("@ss.hasPermi('web.userIdentityPermWrenchProduction.list')")
    public R<List<UserIdentityPermWrenchProduction>> loadList(Long identityRankId) {
        List<UserIdentityPermWrenchProduction> list = basicService.appListByIdentityRankId(identityRankId);
        return R.ok(list);
    }

//    /**
//     *  根据id查询
//     */
//    @GetMapping("/selectById")
//    @ApiOperation(" 根据id查询")
//    public R<UserIdentityPermWrenchProduction> selectById(@RequestParam("id") Long id) {
//        return R.ok(basicService.selectByIdBasic(id));
//    }

    @GetMapping("/getFactoryStatus")
    @PreAuthorize("@ss.hasPermi('web.userIdentityPermWrenchProduction.getFactoryStatus')")
    @ApiOperation(" 获取套餐开关状态")
    public R<Boolean> getFactoryStatus(Long identityRankId) {
        UserIdentityRank identityRank = userIdentityRankService.getById(identityRankId);
        return R.ok(ObjUtil.isEmpty(identityRank)?Boolean.FALSE:identityRank.getIsOpenWrenchProduction());
    }

    /**
     * 开启或关闭扳手工厂
     *
     * @param dto
     * @return
     */
    @PostMapping("/openOrCloseWrenchFactory")
    @PreAuthorize("@ss.hasPermi('web.userIdentityPermWrenchProduction.openOrCloseWrenchFactory')")
    @ApiOperation("修改扳手工厂开关")
    public R openOrCloseWrenchFactory(@Validated @RequestBody OpenOrCloseWrenchFactoryDto dto) {
        basicService.openOrCloseWrenchFactory(dto);
        return R.ok();
    }

    /**
     * 新增或修改扳手套餐
     *
     * @param
     * @return
     */
    @PostMapping("/updateWrenchFactoryConfig")
    @PreAuthorize("@ss.hasPermi('web.userIdentityPermWrenchProduction.updateWrenchFactoryConfig')")
    @ApiOperation("保存扳手生产配置")
    public R updateWrenchFactoryConfig(@Validated @RequestBody UpdateWrenchFactoryConfigDto dto) {
        // 开关状态校验
        basicService.checkFactoryStatus(dto.getIdentityRankId());
        List<UserIdentityPermWrenchProduction> list = dto.getList();
        list.forEach(l -> {
            if (l.getId() == null) {
                basicService.insert(l);
            } else {
                basicService.updateById(l);
            }
        });
        return R.ok();
    }

    /**
     * 删除扳手套餐
     */
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('web.userIdentityPermWrenchProduction.delete')")
    @ApiOperation("删除扳手套餐")
    public R<Boolean> delete(@Validated @RequestBody DeleteWrenchProductionDto dto) {
        basicService.delete(dto);
        return R.ok();
    }

}
